﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using AtlantisClientWPF.Bins;
using LoveMusic.Helpers;
using System.Xml;

namespace AtlantisClientWPF.APIS
{
    public class LastFmAPI
    {
        string apiKey = "b25b959554ed76058ac220b7b2e0a026";
        /// <summary>
        /// Return the stories for the specific artist name. 
        /// </summary>
        ///  // url http://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&artist=cher&api_key=b25b959554ed76058ac220b7b2e0a026
        /// <param name="artistName"></param>
        /// <returns></returns>
        public List<Story> GetSongs(ARTIST artist)
        {
            List<Story> songs = new List<Story>();
            string url = "http://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&artist=" + artist.NAME + "&api_key=" + apiKey;
            string xml = WebHelper.GetResponseStandard(url);

            if (xml == null)
                return songs;

            string songName = string.Empty;
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc = WebHelper.load(xml, xmlDoc, url);

            if (xmlDoc != null)
            {
                foreach (XmlElement element in xmlDoc.GetElementsByTagName("track"))
                {
                    try
                    {
                        // Title fix to not enter empty title
                        string tempTitle = (element.GetElementsByTagName("name").Item(0).InnerXml).Trim();
                        if (tempTitle =="")
                            tempTitle="unknown song name";

                      Story song = new Story
                      {
                             Title = tempTitle,
                             PostedByUserName = "Atlantis",
                             PublishedOn = new DateTime(2007, 3, 1, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond),
                             PostedOn = new DateTime(2007, 3, 1, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond),
                             Playcount = Int32.Parse(element.GetElementsByTagName("playcount").Item(0).InnerXml),
                             Listeners = Int32.Parse(element.GetElementsByTagName("listeners").Item(0).InnerXml),
                             ArtistName = (artist.NAME).Trim(),
                             Description = "This is one of many songs from " + artist.NAME.Trim(),
                             youTubeDescription = "This is "+(element.GetElementsByTagName("name").Item(0).InnerXml).Trim()+" song from " + artist.NAME.Trim(),
                             ARTIST_IDZ = artist.ARTIST_IDZ,
                             CategoryID = artist.Category,
                             Status=0
                        };

                      #region Votes

                      Vote vote = new Vote { StoryID = song.ID, UserID = new Guid("86A13EB9-9CFB-414C-9C12-A580934E86E6"), Timestamp = song.PostedOn };
                      song.Votes.Add(vote);
                          

                      #endregion Votes
                        songs.Add(song);
                    }
                    catch (Exception eX)
                    {
                        throw eX;
                    }
                }
            }

            return songs;
        }
    }
}
